package framework;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;

import com.unitedinternet.portal.selenium.utils.logging.LoggingDefaultSelenium;

public class HelperFunctions {

	public static void focusAndType(LoggingDefaultSelenium selenium,
			String Location, String userInput) {
		selenium.focus(Location);
		selenium.type(Location, userInput);
	}

	public static void focusAndClickAndWait(LoggingDefaultSelenium selenium,
			String Location) {
		selenium.focus(Location);
		selenium.click(Location);
		selenium.waitForPageToLoad("30000");
	}

	public static void focusAndClick(LoggingDefaultSelenium selenium,
			String Location) {
		selenium.focus(Location);
		selenium.click(Location);
	}

		
	public static void clickAndWait(LoggingDefaultSelenium selenium,
			String Location, String verifyElement) {
		long start;   
	    start = System.currentTimeMillis();
		boolean s;
		HelperFunctions.focusAndClick(selenium, Location);
		for (int i = 0; i < 40; i++) {
			s = selenium.isElementPresent(verifyElement);
			if (s) {
				break;
			} else {
				try {
					Thread.sleep(1000);
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}

			}

		}
		 System.out.println("Page Load Time " + (System.currentTimeMillis() - start) + " msec.");
	}

	public static void waitForElement(LoggingDefaultSelenium selenium,
			String verifyElement) {

		boolean s;
		for (int i = 0; i < 30; i++) {
			s = selenium.isElementPresent(verifyElement);
			if (s) {
				break;
			} else {
				try {
					Thread.sleep(1000);
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}

			}

		}
	}

	public static void focusCell(String mapValue, int row) {
		for (int itRow = 1; itRow < row; itRow++) {
			if (itRow == 1) {
				mapValue = mapValue.replaceFirst("trow", Integer
						.toString(itRow));
			} else {
				mapValue = mapValue.replaceFirst(Integer.toString(itRow - 1),
						Integer.toString(itRow));
			}
		}

	}

	public static void AddDataToCell(LoggingDefaultSelenium selenium,
			Map<String, List<String>> resultMap, String actionInitiator,
			String sizeRangeTable) {
		int row = resultMap.size();
		String tableLocator = sizeRangeTable.concat("[trow]//td[Col]");
		int colCount = selenium.getXpathCount(tableLocator.concat("[1]//td"))
				.intValue();
		Set keys = resultMap.keySet();
		for (Iterator i = keys.iterator(); i.hasNext();) {
			Integer key = (Integer) i.next();

			for (int itRow = 1; itRow < row; itRow++) {
				selenium.click(actionInitiator);
				if (itRow == 1) {
					tableLocator = tableLocator.replaceFirst("tr[trow]", "tr["
							+ Integer.toString(itRow) + "]");
				} else {
					tableLocator = tableLocator.replaceFirst("tr["
							+ Integer.toString(itRow - 1) + "]", "tr["
							+ Integer.toString(itRow) + "]");
				}
				for (int j = 1; j <= colCount; j++) {

				}
				selenium.focus(tableLocator);
				selenium.select(tableLocator, resultMap.get(key).get(itRow));
			}
		}

	}
	
	
	public static  Map<Integer, List> getdata(){
		
		Map<Integer, List> mapValue = new HashMap();
		String filepath="D://a1.xlsx" ;
		try {
			InputStream myxls = new FileInputStream(filepath);
			HSSFWorkbook wb = new HSSFWorkbook(myxls);
			HSSFSheet sheet = wb.getSheetAt(0);
			int row_count = sheet.getPhysicalNumberOfRows();
			HSSFRow row = sheet.getRow(0);
			int cell_count = row.getPhysicalNumberOfCells();
			for (int j = 1; j < row_count; j++) {
				mapValue.put(j, new ArrayList());
			}

			for (int i = 1; i < row_count; i++) {
				List list = mapValue.get(i);
				row = sheet.getRow(i);
				for (int j = 0; j < cell_count; j++) {
					HSSFCell cell = row.getCell(j);
					String cellvalue = cell != null ? cell.getStringCellValue()
							: "";
					list.add(cellvalue);
				}

			}
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		// workbook wb= new HSS
		catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		return mapValue;
	}
}
